package login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      //1.从请求中获取到页面提交的用户名和密码
       String username = req.getParameter("username");
       String password = req.getParameter("password");
       //2.验证用户名密码是否正确
        if("zhangsan".equals(username)&&"123".equals(password)){
            //登陆成功
            HttpSession session = req.getSession(true);
            session.setAttribute("username","zhangsan");
            resp.sendRedirect("index");
        }else{
            //登录失败
            resp.setStatus(403);
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().write("登陆失败，用户名或者密码错误");
        }
    }
}
